package cn.kgc.jdbc.dao;

import cn.kgc.jdbc.entity.User;
import cn.kgc.jdbc.utils.DruidUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import java.sql.SQLException;

/**
 * @author: mosin
 * @version: v1.0  2022/4/23
 */
public class UserDaoImpl implements UserDao {
    @Override
    public int addUser(User user) {   //注册
        QueryRunner queryRunner = new QueryRunner(DruidUtil.getDataSource());
        String sql = "insert into usr (uname,pwd) values(?,?) ";
        int line = 0;
        try {
            line = queryRunner.update(sql, user.getUname(), user.getPwd());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return line;
    }

    @Override
    public User selectUser(User user) { // 登录
        QueryRunner queryRunner = new QueryRunner(DruidUtil.getDataSource());
        String sql = "select *   from usr  where uname= ? and pwd = ?";
        try {
            User usr = queryRunner.query(sql, new BeanHandler<>(User.class),user.getUname(),user.getPwd());
            return  usr;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }
}
